import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import org.junit.Test;

import rmi.Connection;
import rmi.Me;
import rmi.UserBuffer;
import rmi.UserInterface;

/**
 * 
 */

/**
 * @author REMY Timothée and CARTIER Arthur
 * 
 */
public class testUser {

	/**
	 * Test method for {@link rmi.User#searchFriend(java.lang.String, int)}.
	 * 
	 * @throws RemoteException
	 * @throws AlreadyBoundException
	 * @throws InterruptedException
	 */
	@Test
	public void testSearchFriend() throws RemoteException,
			AlreadyBoundException, InterruptedException {
		Registry r1, r2, r3;
		r1 = LocateRegistry.createRegistry(1111);
		r2 = LocateRegistry.createRegistry(2222);
		r3 = LocateRegistry.createRegistry(3333);
		final Me moi = new Me("moi", "localhost", 1111);
		final Me a = new Me("a", "localhost", 2222);
		final Me b = new Me("b", "localhost", 3333);

		Connection cmoi, ca, cb;

		cmoi = new Connection(1111, r1, moi);
		ca = new Connection(2222, r2, a);
		cb = new Connection(3333, r3, b);

		r1.bind("connect", cmoi);
		r2.bind("connect", ca);
		r3.bind("connect", cb);

		moi._setConnection(cmoi);
		a._setConnection(ca);
		b._setConnection(cb);

		moi.getFriends().put("a", new UserBuffer("a", "localhost", 2222));
		moi.getFriends().put("b", new UserBuffer("b", "localhost", 3333));

		moi.connectAllDisconnectedFriends();

		Thread.sleep(1000);

		System.out.println("Amis de a");
		for (final String s : a.getFriends().keySet()) {
			System.out.println(s);
		}
		System.out.println("Amis de b");
		for (final String s : b.getFriends().keySet()) {
			System.out.println(s);
		}

		UserInterface rechercheA = moi.searchFriend("a", 10);

		System.out.println("\n moi recherche <<a>> et trouve :"
				+ rechercheA.getPseudo() + " (" + rechercheA.getIp() + ":"
				+ rechercheA.getPort() + ")");
		rechercheA = b.searchFriend("a", 10);
		System.out.println("\n b recherche <<a>> et trouve :"
				+ rechercheA.getPseudo() + " (" + rechercheA.getIp() + ":"
				+ rechercheA.getPort() + ")");
	}

}
